package com.gotloop.controllers;

import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.gotloop.jdo.Loop;
import com.gotloop.services.LoopService;

/**
 * Controller to manage the loop upload.
 * @author jibhaine
 *
 */
public class LoopUploadController {

	/**
	 * Logger for LoopUploadController.
	 */
	private static final Logger LOG = LoggerFactory.getLogger(LoopUploadController.class);
	
	private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

	private UserService userService = UserServiceFactory.getUserService();

	private LoopService loopService;
	
	/**
	 * Provides the loop upload form.
	 * @return the loop upload form
	 */
	@RequestMapping(value = { "/loop/upload" }, method = RequestMethod.GET)
	public ModelAndView getLoopUploadForm() {
		String uploadUrl = blobstoreService.createUploadUrl("/loop/upload");
		ModelAndView mv = new ModelAndView("loopUploadForm");
		mv.addObject(uploadUrl);
		return mv;		
	}

	/**
	 * Manage the loop upload.
	 * @return the successful upload page or the loop upload form.
	 */
	@RequestMapping(value = { "/loop/upload" }, method = RequestMethod.POST)
	public ModelAndView uploadLoop(HttpServletRequest req, HttpServletResponse res) {

	    try {
	      ServletFileUpload upload = new ServletFileUpload();
	      res.setContentType("text/plain");

	      FileItemIterator iterator = upload.getItemIterator(req);
	      while (iterator.hasNext()) {
	        FileItemStream item = iterator.next();
	        InputStream stream = item.openStream();

	        if (item.isFormField()) {
	          LOG.trace("Got a form field: " + item.getFieldName());
	        } else {
	        	LOG.trace("Got an uploaded file: " + item.getFieldName() +
	                      ", name = " + item.getName()+ ", content-type = " +
	        	item.getContentType());
	        	
	        }
	      }
	    } catch (Exception ex) {
	    	LOG.error(ex.getMessage());
	    	String error = ex.getMessage();
	    	return new ModelAndView("loopUploadKO").addObject(error);
	    }

		return new ModelAndView("loopUploadOK");
	}
}
